//
//  BMHCommentIssueView.m
//  BMHVideoMain
//
//  Created by 陈宪栋 on 2017/3/23.
//  Copyright © 2017年 Baomihua. All rights reserved.
//

#import "BMHCommentIssueView.h"
#import <UtilsMacro.h>
#import <UIView+Layout.h>
#import <ReactiveCocoa.h>
#import <MBProgressHUD+BMH.h>

@interface BMHCommentIssueView ()<UITextViewDelegate>
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UIView *foregroundView;
@property (nonatomic, strong) UIView *whiteView;
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UIButton *issueBtn;
@property (nonatomic, strong) UITapGestureRecognizer *tap;
@property (nonatomic, strong) UILabel *placeholderLabel;
@end

@implementation BMHCommentIssueView

- (RACSubject *)submitCommentSignal {
    if (_submitCommentSignal == nil) {
        _submitCommentSignal = [RACSubject subject];
    }
    return _submitCommentSignal;
}

- (instancetype)init {
    self = [super initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    if (self) {
        [self setupView];
        self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickOut:)];
        [self.backgroundView addGestureRecognizer:self.tap];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
        RAC(self.issueBtn,enabled) = [self.textView.rac_textSignal map:^id(NSString *text) {
            return @(text.length > 0);
        }];
        RAC(self.placeholderLabel,hidden) = [self.textView.rac_textSignal map:^id(NSString *text) {
            return @(text.length > 0);
        }];
    }
    return self;
}

- (void)setupView {
    CGFloat ForegroundHeight = ScreenHeight == 480 ? 120 : 180;
    
    self.backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    self.backgroundView.backgroundColor = [UIColor blackColor];
    self.backgroundView.alpha = 0;
    [self addSubview:self.backgroundView];
    
    self.foregroundView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, ForegroundHeight)];
    self.foregroundView.backgroundColor = RGB(242, 242, 242);
    [self addSubview:self.foregroundView];
    
    self.whiteView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, ScreenWidth - 40, ForegroundHeight - 70)];
    if (ScreenHeight == 480) {
        self.whiteView.frame = CGRectMake(10, 10, ScreenWidth - 20, ForegroundHeight - 50);
    }
    self.whiteView.backgroundColor = [UIColor whiteColor];
    [self.foregroundView addSubview:self.whiteView];
    
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 0, self.whiteView.width - 20, self.whiteView.height)];
    self.textView.delegate = self;
    self.textView.backgroundColor = [UIColor greenColor];
    self.textView.font = [UIFont systemFontOfSize:14.0];
    self.textView.backgroundColor = [UIColor whiteColor];
    [self.whiteView addSubview:self.textView];
    
    self.placeholderLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, self.textView.width - 10, 30)];
    self.placeholderLabel.font = [UIFont systemFontOfSize:14];
    self.placeholderLabel.textColor = RGB(173, 173, 173);
    [self.textView addSubview:self.placeholderLabel];
    
    self.issueBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth - 100, self.whiteView.bottom + 10, 80, 30)];
    if (ScreenHeight == 480) {
        self.issueBtn.frame = CGRectMake(ScreenWidth - 90, self.whiteView.bottom + 5, 80, 30);
    }
    self.issueBtn.layer.cornerRadius = 2;
    self.issueBtn.layer.masksToBounds = YES;
    self.issueBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [self.issueBtn setTitle:@"发表" forState:UIControlStateNormal];
    [self.issueBtn setBackgroundColor:RGB(49, 145, 213)];
    [self.issueBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.issueBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [self.issueBtn addTarget:self action:@selector(issueAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.foregroundView addSubview:self.issueBtn];
}

- (void)updatePlaceholderText:(NSString *)placeholderText {
    self.placeholderLabel.text = placeholderText;
}


#pragma mark - EventResponse
- (void)clickOut:(UITapGestureRecognizer *)tap {
    CGFloat currentTop = [tap locationInView:self].y;
    CGFloat timerViewTop = ScreenHeight - self.foregroundView.height;
    if (currentTop < timerViewTop) {
        [self hide];
    }
}

- (void)show {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    [self.textView becomeFirstResponder];
}

- (void)hide {
    [self.textView resignFirstResponder];
}

- (void)issueAction:(UIButton *)btn {
    [self.submitCommentSignal sendNext:self.textView.text];
    [self hide];
}

- (void)keyboardWillShow:(NSNotification *)note {
    NSDictionary *userInfo = note.userInfo;
    CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGRect keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat keyboardHeight = keyboardRect.size.height;
    [self showKeyboardAnimation:YES keyboardHeight:keyboardHeight duration:duration];
}

- (void)keyboardHide:(NSNotification *)note {
    NSDictionary *userInfo = note.userInfo;
    CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    [self showKeyboardAnimation:NO keyboardHeight:0 duration:duration];
}

#pragma --mark-- 键盘出现和隐藏时的动画
- (void)showKeyboardAnimation:(BOOL)isShow keyboardHeight:(CGFloat)keyboardHeight duration:(CGFloat)duration //yes表示显示键盘
{
    if(isShow) {
        [UIView animateWithDuration:duration + 0.3 animations:^{
            self.backgroundView.alpha = 0.35;
            self.foregroundView.bottom = ScreenHeight - keyboardHeight;
        }];
    }else{
        [UIView animateWithDuration:duration + 0.3 animations:^{
            self.foregroundView.top = ScreenHeight;
            self.backgroundView.alpha = 0;
        } completion:^(BOOL finished) {
            if (finished) {
                [self.backgroundView removeGestureRecognizer:self.tap];
                [self removeFromSuperview];
            }
        }];
    }
}

#pragma mark - UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    textView.text = @"";
    return YES;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    // 不让输入表情
    if ([textView isFirstResponder]) {
        if ([[[textView textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textView textInputMode] primaryLanguage]) {
            [MBProgressHUD showError:@"暂不支持表情功能!"];
            return NO;
        }
    }
    
    //    if ([text isEqualToString:@"\n"]) {//按下return键
    //        //发表评论
    //        [self.submitCommentSignal sendNext:textView.text];
    //        return NO;
    //    }
    return YES;
}

#pragma mark - dealloc
- (void)dealloc {
    NSLog(@"%@ dealloc",[self class]);
}

@end
